package com.ruoyi.qd.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qd.vo.RobotQueuePageReq;
import com.ruoyi.qd.vo.RobotQueuePageRes;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qd.domain.RobotQueue;
import com.ruoyi.qd.service.IRobotQueueService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.mybatisflex.core.paginate.Page;
import com.ruoyi.common.core.page.PageDomain;

/**
 * 机器人队列Controller
 * 
 * @author tushiguang
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/qd/queue")
public class RobotQueueController extends BaseController {
    @Autowired
    private IRobotQueueService robotQueueService;

    /**
     * 根据机器人 sn 获取实时队列情况
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:listRobotQueueNow')")
    @GetMapping("/listRobotQueueNow/{robotSn}")
    public AjaxResult listRobotQueueNow(@PathVariable String robotSn) {
        return success(robotQueueService.listRobotQueueNow(robotSn));
    }

    /**
     * 删除机器人队列
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:remove')")
    @Log(title = "机器人队列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        robotQueueService.deleteRobotQueueByIds(ids);
        return success();
    }

    /**
     * 根据机器人 id 获取已经预约的时间段
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:listRobotAppointmentByRobotId')")
    @GetMapping("/listRobotAppointmentByRobotId/{robotId}")
    public AjaxResult listRobotAppointmentByRobotId(@PathVariable Long robotId) {
        return success(robotQueueService.listRobotAppointmentByRobotId(robotId));
    }

    /**
     * 查询机器人队列列表
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageDomain pageDomain, RobotQueuePageReq robotQueuePageReq) {
        Page<RobotQueuePageRes> page = robotQueueService.selectRobotQueuePage(pageDomain, robotQueuePageReq);
        return getFlexTable(page);
    }

    /**
     * 导出机器人队列列表
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:export')")
    @Log(title = "机器人队列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RobotQueue robotQueue) {
        List<RobotQueue> list = robotQueueService.selectRobotQueueList(robotQueue);
        ExcelUtil<RobotQueue> util = new ExcelUtil<RobotQueue>(RobotQueue.class);
        util.exportExcel(response, list, "机器人队列数据");
    }

    /**
     * 获取机器人队列详细信息
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(robotQueueService.selectRobotQueueById(id));
    }

    /**
     * 新增机器人队列
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:add')")
    @Log(title = "机器人队列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RobotQueue robotQueue) {
        return toAjax(robotQueueService.insertRobotQueue(robotQueue));
    }

    /**
     * 修改机器人队列
     */
    @PreAuthorize("@ss.hasPermi('qd:queue:edit')")
    @Log(title = "机器人队列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RobotQueue robotQueue) {
        return toAjax(robotQueueService.updateRobotQueue(robotQueue));
    }
}
